package com.itboye.sunsun.controller;

import android.app.Activity;
import android.text.TextUtils;

import com.itboye.sunsun.application.App;
import com.itboye.sunsun.constants.SPContants;
import com.itboye.sunsun.utils.DebugLog;
import com.itboye.sunsun.utils.SPUtils;
import com.umeng.message.IUmengRegisterCallback;
import com.umeng.message.MsgConstant;
import com.umeng.message.PushAgent;
import com.umeng.message.UmengRegistrar;
/**
 * 登录相关控制类--状态模式</br></br>
 * 
 * 登录成功后调用setLoginState(new LoginedState());</br></br>
 * 退出登录后调用setLoginState(new UnLoginState());</br></br>
 * 进入主Activity前一定要设置一次，要么登录要么未登录</br></br>
 * @author Young
 *
 */
public class LoginController {

	private static ILoginState loginState=new UnLoginState();

	public static void setLoginState(ILoginState loginState) {
		
		LoginController.loginState = loginState;
		if (loginState instanceof UnLoginState) {
			DebugLog.v("bytag", "未登录。。。");
//			PushAgent mPushAgent = PushAgent.getInstance(App.ctx);
//			mPushAgent.disable(new IUmengUnregisterCallback() {
//				
//				@Override
//				public void onUnregistered(String arg0) {
//					// TODO Auto-generated method stub
//					DebugLog.v("bytag", "停止推送:"+arg0);
//				}
//			});
		}else if (loginState instanceof LoginedState) {
			DebugLog.v("bytag", "已登录。。。");
			PushAgent mPushAgent = PushAgent.getInstance(App.ctx);
			//mPushAgent.setPushIntentServiceClass(MyPushIntentService.class);
			mPushAgent.setNotificationPlaySound(MsgConstant.NOTIFICATION_PLAY_SDK_ENABLE);
			mPushAgent.onAppStart();
			//开启推送并设置注册的回调处理
			if (!mPushAgent.isEnabled()) {
				mPushAgent.enable(new IUmengRegisterCallback() {

					@Override
					public void onRegistered(String registrationId) {
						//onRegistered方法的参数registrationId即是device_token
						
						setExclusiveAlias(registrationId);
					}
				});
			}
			
			String device_token = UmengRegistrar.getRegistrationId(App.ctx);
			if (!TextUtils.isEmpty(device_token)) {
				setExclusiveAlias(device_token);
			}
		}
	}

	private static void setExclusiveAlias(String registrationId){
		String uid=(String)SPUtils.get(App.ctx, null, SPContants.USER_ID, "");
		PushAgent mPushAgent = PushAgent.getInstance(App.ctx);
		mPushAgent.setExclusiveAlias(uid, "sunsun");
		DebugLog.v("bytag", "setExclusiveAlias   token:",registrationId," alian:",uid, "  type:","sunsun");
		 
//		//登录后绑定设备
//		MyJsonRequest<String> request = new MyJsonRequest.Builder<String>().apiVer("100")
//				.typeKey("BY_Device_bind")
//				.param("uid", (String) SPUtils.get(App.ctx, null, SPContants.USER_ID, ""))
//				.param("device_token",registrationId)
//				.param("version",android.os.Build.VERSION.RELEASE)
//				.param("device_type","Android")
//				.requestListener(new XRequestListener<String>() {
//
//					@Override
//					public void onResponse(String response) {
//						 
//					}
//				}).errorListener(new XErrorListener() {
//
//					@Override
//					public void onErrorResponse(Exception exception, int code, String msg) {
//						if (exception instanceof CodeErrorException) {
//							Toast.makeText(App.ctx, msg, 0).show();
//						} else {
//							Toast.makeText(App.ctx, exception.toString(), 0).show();
//						}
//
//					}
//				}).build();
//		HttpRequest.getDefaultRequestQueue().add(request);
//	 
 
	}
	/////////// 购物相关/////////
	public static void onShopMessageClick(Activity act,Object data) {
		loginState.onShopMessageClick(act,data);
	}
	public static void onShopMoreClick(Activity act,Object data) {
		loginState.onShopMoreClick(act,data);
	}
	public static void onShopCarClick(Activity act,Object data) {
		loginState.onShopCarClick(act,data);
	}

	public static void onShopCustomerServiceClick(Activity act,Object data) {
		loginState.onShopCustomerServiceClick(act,data);
	}

	public static void onGoodsCollectClick(Activity act,Object data) {
		loginState.onGoodsCollectClick(act,data);
	}

	public static void onAddShopCarClick(Activity act,Object data) {
		loginState.onAddShopCarClick(act,data);
	}

	public static void onBuyClick(Activity act,Object data) {
		loginState.onBuyClick(act,data);
	}

	public static void onGoodsShareClick(Activity act,Object data) {
		loginState.onGoodsShareClick(act,data);
	}

	/////////////// 论坛相关/////////////////
	public static void onBBSJuBaoClick(Activity act,Object data) {
		loginState.onBBSJuBaoClick(act,data);
	}
	
	public static void onBBSMySelfClick(Activity act,Object data) {
		loginState.onBBSMySelfClick(act,data);
	}

	public static void onBBSMessageClick(Activity act,Object data) {
		loginState.onBBSMessageClick(act,data);
	}

	/**
	 * 论坛发帖
	 * 
	 * @param act
	 */
	public static void onBBSPublishMessageClick(Activity act,Object data) {
		loginState.onBBSPublishMessageClick(act,data);
	}

	public static void onBBSCommentClick(Activity act,Object data) {
		loginState.onBBSCommentClick(act,data);
	}

	public static void onBBSPraiseClick(Activity act,Object data) {
		loginState.onBBSPraiseClick(act,data);
	}

 
	/**
	 * 收藏帖子
	 * @param act
	 * @param data
	 */
	public static void onBBSCollectClick(Activity act,Object data) {
		loginState.onBBSCollectClick(act,data);
	}
	

	public static void onMyWealthClick(Activity act,Object data) {
		loginState.onMyWealthClick(act,data);
	}
	
	public static void onAllOrderClick(Activity act,Object data) {
		loginState.onAllOrderClick(act,data);
	}
	
	public static void onPointMarketClick(Activity act,Object data) {
		loginState.onPointMarketClick(act,data);
	}
	
	public static void onJiangClick(Activity act,Object data) {
		loginState.onJiangClick(act,data);
	}
	
	public static void onMyWordsClick(Activity act,Object data) {
		loginState.onMyWordsClick(act,data);
	}
	
	public static void onSignClick(Activity act,Object data) {
		loginState.onSignClick(act,data);
	}
	
	public static void onPersonalsettingClick(Activity act,Object data) {
		loginState.onPersonalsettingClick(act,data);
	}
	
	public static void onMyCollectClick(Activity act,Object data) {
		loginState.onMyCollectClick(act,data);
	}
	
	public static void onMyLocationClick(Activity act,Object data) {
		loginState.onMyLocationClick(act,data);
	}
	
	public static void onCustomClick(Activity act,Object data) {
		loginState.onCustomClick(act,data);
	}
	
	public static void onPayClick(Activity act,Object data) {
		loginState.onPayClick(act,data);
	}
	
	public static void onSendClick(Activity act,Object data) {
		loginState.onSendClick(act,data);
	}
	
	public static void onReceiveClick(Activity act,Object data) {
		loginState.onReceiveClick(act,data);
	}
	
	
}
